package com.yuexunit.finance.statement.service;

import java.util.Date;
import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.yuexunit.finance.common.util.RandomUtil;
import com.yuexunit.finance.project.SettingConstants;
import com.yuexunit.finance.statement.entity.DaysEarningEntity;
import com.yuexunit.inject.annotation.Implements;
import com.yuexunit.page.PageCriteria;
import com.yuexunit.page.PageList;
import com.yuexunit.persist.PersistUtil;
import com.yuexunit.persist.annotation.Transactional;
import com.yuexunit.util.DateUtil;
import com.yuexunit.util.StringUtil;

/**
*@version 创建时间：2017年12月6日 上午9:49:21
*@author wenwenlong
*类说明
*/

@Implements(DaysEarningService.class)
public class DaysEarningServiceImpl implements DaysEarningService{
	
	@Override
	@Transactional
	public Long createDaysEarning(DaysEarningEntity entity) {
		entity.setDaysEarningNumber(SettingConstants.PRE_NUMBER_DAYS_EARNING + RandomUtil.generateTimeNumber());
		entity.setCreateDt(DateUtil.now());
		entity.setUpdateDt(DateUtil.now());
		entity.setEnabled(Boolean.TRUE);
		PersistUtil.insertOrUpdate(entity);
		return entity.getDaysEarningId();
	}
	

	@Override
	public DaysEarningEntity inquireDaysEarningDetail(Long daysEarningId , Integer interestDate, Long financeId) {
		DetachedCriteria criteria = DetachedCriteria.forClass(DaysEarningEntity.class);
		if(null !=daysEarningId){
			criteria.add(Restrictions.eq("daysEarningId", daysEarningId));
		}
		if(null !=interestDate){
			criteria.add(Restrictions.eq("interestDate", interestDate));
		}
		if(financeId !=null){
			criteria.add(Restrictions.eq("financeId", financeId));
		}
//		criteria.addOrder(Order.asc("earningSummaryId"));
		List<DaysEarningEntity> list = PersistUtil.get(criteria);
		Integer last = list.size() - 1;
		if (list.isEmpty()) {
			return null;
		} else {
			return list.get(last);
		}
	}
	
	@Override
	public List<DaysEarningEntity> inquireDaysEarningList(Long financeId, Integer date, String financeNumber, String enterpriseName) {
		DetachedCriteria criteria = DetachedCriteria.forClass(DaysEarningEntity.class);
		if(financeId != null){
			criteria.add(Restrictions.eq("financeId", financeId));
		}
		
		if(date != null){
			criteria.add(Restrictions.eq("interestDate", date));
		}
		
		if(StringUtil.isNotBlank(financeNumber)){
			criteria.add(Restrictions.eq("financeNumber", financeNumber));
		}
		
		if(StringUtil.isNotBlank(enterpriseName)){
			criteria.add(Restrictions.eq("enterpriseName", enterpriseName));
		}
		
		return PersistUtil.get(criteria);
	}
	
	@Override
	public PageList<DaysEarningEntity> inquireDaysEarningPageList(Double interest, String enterpriseName, Integer createDateTimeBegins,
			Integer createDateTimeEnds, Long financeId, String financeNumber, Long enterpriseId, PageCriteria page) {
		DetachedCriteria criteria = DetachedCriteria.forClass(DaysEarningEntity.class);
		if(interest!=null){
			criteria.add(Restrictions.eq("interest", interest));
		}
		if (StringUtil.isNotBlank(enterpriseName)) {
			criteria.add(Restrictions.like("enterpriseName",  "%"+enterpriseName+"%"));
		}
		if (null != financeId) {
			criteria.add(Restrictions.eq("financeId", financeId));
		}
		if (createDateTimeBegins != null && createDateTimeEnds!=null) {
			criteria.add(Restrictions.ge("interestDate",createDateTimeBegins));
		}else{
			criteria.add(Restrictions.eq("interestDate", createDateTimeBegins));
		}
		if (createDateTimeBegins != null && createDateTimeEnds!=null) {
			criteria.add(Restrictions.le("interestDate",createDateTimeEnds));
		}
		if (StringUtil.isNotBlank(financeNumber)) {
			criteria.add(Restrictions.like("financeNumber", "%"+financeNumber+"%"));
		}
		if (null != enterpriseId) {
			criteria.add(Restrictions.eq("enterpriseId", enterpriseId));
		}
		criteria.add(Restrictions.eq("enabled", Boolean.TRUE));
		if(interest !=null && enterpriseId !=null){
			criteria.addOrder(Order.desc("loanDate"));
			criteria.addOrder(Order.desc("financeNumber"));
		}
		criteria.addOrder(Order.desc("enterpriseName"));
		criteria.addOrder(Order.desc("loanDate"));
		criteria.addOrder(Order.desc("financeNumber"));

		return PersistUtil.get(criteria, page);
	}


	@Override
	public PageList<DaysEarningEntity> inquireMonthDaysEarningPageList(
			String financeNumber, Date earningDateStart, Date earningDateEnd,
			PageCriteria page) {
		DetachedCriteria criteria = DetachedCriteria.forClass(DaysEarningEntity.class);
		if(financeNumber != null){
			criteria.add(Restrictions.eq("financeNumber", financeNumber));
		}
		if (earningDateStart != null && earningDateEnd != null) {
			criteria.add(Restrictions.between("createDt", earningDateStart, earningDateEnd));
		}
		criteria.addOrder(Order.desc("createDt"));
		criteria.add(Restrictions.eq("enabled", Boolean.TRUE));
		return PersistUtil.get(criteria, page);
	}
}
